<?php

namespace app\api\controller\kuyou\applet;

use think\Db;

class AccountsLog extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $map = [];
            $pay_time = $this->request->post('pay_time');
            if(!empty($pay_time)){
                $str = explode('~',$pay_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['pay_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $accounts = $this->request->post('accounts');
            if(!empty($accounts)){
                $map['id'] = ['in',$accounts];
            }
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $where['customers_id'] = ['in',$customers_id];
            }
            $suppliers_id = $this->request->post('suppliers_id');
            if(!empty($suppliers_id)){
                $suppliers = Db::name('kuyou_suppliers')->where(['id'=>['in',$suppliers_id]])->column('supplyer_code');
                $where['supplyer_code'] = ['in',$suppliers];
            }
            $payment = $this->request->post('payment');
            if(!empty($payment)){
                $where['payment'] = ['in',$payment];
            }

            $sup = Db::name('kuyou_accounts')
                ->where($map)
                ->order('account_coding asc')
                ->select();

            $field = empty(input('field'))?'createtime':trim(input('field'));
            $order = empty(input('sort'))?'ASC':trim(input('sort'));

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $price_decimal  = $setList['price_decimal']['value'];
            $stmen = Db::name('kuyou_staff_member')->column('id','name');


            $listarr = [];
            $all_income = 0;
            $all_expenditure = 0;
            $all_balance = 0;
            foreach ($sup as $sup_k=>$sup_v){
                $income = 0;
                $expenditure = 0;
                $balance = 0;
                $listarr[] = [
                    'account_code'  => $sup_v['account_coding'],
                    'account_name'  => $sup_v['account_name'],
                    'pay_time'      => '',
                    'bill_code'     => '',
                    'business_type' => '期初余额',
                    's_type'        => 1,
                    'income'        => '',
                    'expenditure'   => '',
                    'balance'       => round($sup_v['start_account_balance'],$price_decimal),
                    'correspondent' => '',
                    'staff_member'  => '',
                    'remark'        => ''
                ];
                $balance += $sup_v['start_account_balance'];

                $list = Db::name('kuyou_accounts_log')
                    ->where($where)
                    ->where(['account_coding'=>$sup_v['account_coding']])
                    ->order([$field=>$order])
                    ->select();
                foreach ($list as $list_k=>$list_v){
                    $balance        += $list_v['income'];
                    $balance        -= $list_v['expenditure'];
                    $income         += $list_v['income'];
                    $expenditure    += $list_v['expenditure'];
                    $correspondent = '';
                    if(in_array($list_v['business_type'],[2,3,4,8,10])){
                        $correspondent = Db::name('kuyou_suppliers')->where(['supplyer_code'=>$list_v['supplyer_code']])->value('supplyer_name');
                    }else if(in_array($list_v['business_type'],[5,6,7,9])){
                        $correspondent = Db::name('kuyou_customers')->where(['id'=>$list_v['customers_id']])->value('customer_name');
                    }
                    $listarr[] = [
                        'account_code'  => $sup_v['account_coding'],
                        'account_name'  => $sup_v['account_name'],
                        'pay_time'      => !empty($list_v['pay_time'])?date('Y-m-d',$list_v['pay_time']):'',
                        'bill_code'     => $list_v['bill_code'],
                        'business_type' => $list_v['business_type'],
                        's_type'        => 1,
                        'income'        => $list_v['income'],
                        'expenditure'   => $list_v['expenditure'],
                        'balance'       => round($balance,$price_decimal),
                        'correspondent' => $correspondent,
                        'staff_member'  => isset($stmen[$list_v['payment']])?$stmen[$list_v['payment']]:'',
                        'remark'        => $list_v['remark'],
                    ];
                }
                $listarr[] = [
                    'account_code'  => $sup_v['account_coding'],
                    'account_name'  => $sup_v['account_name'],
                    'pay_time'      => '',
                    'bill_code'     => '',
                    'business_type' => '小计',
                    's_type'        => 2,
                    'income'        => round($income,$price_decimal),
                    'expenditure'   => round($expenditure,$price_decimal),
                    'balance'       => round($balance,$price_decimal),
                    'correspondent' => '',
                    'staff_member'  => '',
                    'remark'        => ''
                ];
                $all_balance        += $balance;
                $all_income         += $income;
                $all_expenditure    += $expenditure;
            }

            $counts = [];
            $counts['account_code'] = '';
            $counts['account_name'] = '';
            $counts['pay_time'] = '合计';
            $counts['bill_code'] = '';
            $counts['business_type'] = '合计';
            $counts['s_type'] = 3;
            $counts['income'] = $all_income;
            $counts['expenditure'] = $all_expenditure;
            $counts['balance'] = $all_balance;
            $counts['correspondent'] = '';
            $counts['staff_member'] = '';
            $counts['remark'] = '';

            $listarr[] = $counts;

            $result = array("count" => sizeof($listarr), "list" => $listarr);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
